package indi.zhifa.study2025.oa.service.menu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.engine.core.common.utils.DtoEntityUtil;
import indi.zhifa.study2025.oa.dao.gen.service.IGenMenuDbService;
import indi.zhifa.study2025.oa.dao.po.GenMenuEntity;
import indi.zhifa.study2025.oa.model.expose.menu.vo.MenuTreeNode;
import indi.zhifa.study2025.oa.service.menu.IMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class MenuServiceImpl implements IMenuService {

    final IGenMenuDbService mGenMenuDbService;

    @Override
    public GenMenuEntity create(GenMenuEntity pGenMenuEntity) {

        LambdaQueryWrapper<GenMenuEntity> queryWrapper = new LambdaQueryWrapper<GenMenuEntity>()
                .eq(GenMenuEntity::getParentId, pGenMenuEntity.getParentId())
                .eq(GenMenuEntity::getCode, pGenMenuEntity.getCode());
        if(mGenMenuDbService.exists(queryWrapper)) {
            throw new ServiceException("已经存在在"+pGenMenuEntity.getParentId()+"下code为"+pGenMenuEntity.getCode()+"的菜单");
        }


        if(!mGenMenuDbService.save(pGenMenuEntity)){
            throw new ServiceException("存储GenMenuEntity失败");
        }
        return pGenMenuEntity;
    }

    @Override
    public GenMenuEntity edit(Long id, Object pMenuDto) {
        GenMenuEntity genMenuEntity = mGenMenuDbService.updatePull(id, pMenuDto);
        return genMenuEntity;
    }

    @Override
    public GenMenuEntity delete(Long pId) {
        GenMenuEntity deletingEntity = mGenMenuDbService.checkMin(pId);
        LambdaUpdateWrapper<GenMenuEntity> updateWrapper = Wrappers.<GenMenuEntity>lambdaUpdate()
                .likeRight(GenMenuEntity::getCodeCascade,deletingEntity.getCodeCascade());
        mGenMenuDbService.remove(updateWrapper);
        return deletingEntity;
    }

    @Override
    public GenMenuEntity check(Long pId) {
        return mGenMenuDbService.check(pId);
    }

    @Override
    public GenMenuEntity checkMin(Long pId) {
        return mGenMenuDbService.checkMin(pId);
    }

    @Override
    public MenuTreeNode getTree(Long pId) {

        GenMenuEntity genMenuEntity = mGenMenuDbService.check(pId);
        LambdaQueryWrapper<GenMenuEntity> queryWrapper = new LambdaQueryWrapper<GenMenuEntity>()
                .likeRight(GenMenuEntity::getCodeCascade, genMenuEntity.getCodeCascade());
        List<GenMenuEntity> genMenuEntityList = mGenMenuDbService.list(queryWrapper);
        MenuTreeNode rootNode = DtoEntityUtil.trans(genMenuEntity,MenuTreeNode.class);
        if(CollectionUtils.isEmpty(genMenuEntityList)){
            return rootNode;
        }
        Map<Long,List<GenMenuEntity>> menuEntityGroup = genMenuEntityList.stream().collect(Collectors.groupingBy(GenMenuEntity::getParentId));
        Queue<MenuTreeNode> que = new ArrayDeque();
        que.add(rootNode);
        while (!que.isEmpty()) {
            MenuTreeNode node = que.poll();
            List<GenMenuEntity> menuEntityList = menuEntityGroup.get(node.getId());
            List<MenuTreeNode> children = DtoEntityUtil.trans(menuEntityList,MenuTreeNode.class);
            node.setChildren(children);
            for(MenuTreeNode child : children) {
                que.add(child);
            }
        }
        return rootNode;
    }
}
